{% extends "layouts/default.html.twig" %}

{% block title %}{{ 'Jobs report'|trans }}{% endblock %}

{% block body %}
    <div class="container-fluid" id="jobsreport">

        {{ include('partials/_page_header.html.twig', {header: 'Jobs report'|trans, description: 'Bacula jobs overview'|trans }) }}

        <div class="row">
            <div class="col-xs-12 col-sm-3 col-sm-push-9 col-lg-2 col-lg-push-10">

                <form class="form" role="form" action="{{ url_for('jobs') }}" method="post">

                    <span class="help-block">{{ 'Filter'|trans }}</span>

                    <div class="form-group">
                        <label>{{ 'Job status'|trans }}</label>
                        <select class="form-control input-sm" name="filter_jobstatus">
                            {% for id, status in job_status %}
                                <option value="{{ id }}" {% if id == filter_jobstatus %} selected {% endif %}>{{ status }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label>{{ 'Level'|trans }}</label>
                        <select class="form-control input-sm" name="filter_joblevel">
                            {% for id, level in levels_list %}
                                <option value="{{ id }}" {% if id == filter_joblevel %} selected {% endif %}>{{ level }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label>{{ 'Type'|trans }}</label>
                        <select class="form-control input-sm" name="filter_jobtype">
                            {% for id, job_type in job_types_list %}
                                <option value="{{ id }}" {% if id == filter_jobtype %} selected {% endif %}>{{ job_type }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label>{{ 'Client'|trans }}</label>
                        <select class="form-control input-sm" name="filter_clientid">
                            {% for id, client in clients_list %}
                                <option value="{{ id }}" {% if id == filter_clientid %} selected {% endif %}>{{ client }}</option>
                            {% endfor %}
                        </select>
                    </div>

                    <div class="form-group">
                        <label>{{ 'Pool'|trans }}</label>
                        <select class="form-control input-sm" name="filter_poolid">
                            {% for id, pool in pools_list %}
                                <option value="{{ id }}" {% if id == filter_poolid %} selected {% endif %}>{{ pool }}</option>
                            {% endfor %}
                        </select>

                    </div>

                    <div class="form-group">
                        <label for="datetimepicker1Input" class="form-label">{{ 'Start time'|trans }}</label>
                        <div class="input-group log-event" id="datetimepicker1" data-td-target-input="nearest"
                             data-td-target-toggle="nearest">
                            <input id="datetimepicker1Input" name="filter_job_starttime" type="text"
                                   class="form-control" data-td-target="#datetimepicker1"
                                   value="{{ filter_job_starttime }}"/>
                            <span class="input-group-text" data-td-target="#datetimepicker1"
                                  data-td-toggle="datetimepicker"> <i class="fas fa-calendar"></i> </span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="datetimepicker1Input" class="form-label">{{ 'End time'|trans }}</label>
                        <div class="input-group log-event" id="datetimepicker2" data-td-target-input="nearest"
                             data-td-target-toggle="nearest">
                            <input id="datetimepicker1Input" name="filter_job_endtime" type="text" class="form-control"
                                   data-td-target="#datetimepicker2" value="{{ filter_job_endtime }}"/>
                            <span class="input-group-text" data-td-target="#datetimepicker2"
                                  data-td-toggle="datetimepicker"> <i class="fas fa-calendar"></i> </span>
                        </div>
                    </div>

                    <span class="help-block">{{ 'Options'|trans }}</span>

                    <label>{{ 'Order by'|trans }}</label>
                    <select class="form-control input-sm" name="filter_job_orderby">
                        {% for id, order in result_order %}
                            <option value="{{ id }}" {% if id == result_order_field %} selected {% endif %}>{{ order }}</option>
                        {% endfor %}
                    </select>

                    <div class="checkbox">
                        <label>
                            <input type="checkbox" name="filter_job_orderby_asc"
                                   value="ASC" {{ result_order_asc_checked }}> Asc
                        </label>
                    </div>

                    <button type="reset" class="btn btn-default btn-sm" title="{{ 'Reset'|trans }}"></button>
                    <button type="submit" class="btn btn-primary btn-sm pull-right"
                            title="{{ 'Apply filter and options'|trans }}">{{ 'Apply'|trans }}</button>

                    <a class="btn btn-link btn-sm" title="{{ 'Reset to default'|trans }}" href="{{ base_path() }}/jobs"
                       role="button">{{ 'Reset to default'|trans }}</a>
                    {{ csrf|raw }}
                </form>

            </div>

            <div class="col-xs-12 col-sm-9 col-sm-pull-3 col-lg-10 col-lg-pull-2">
                <div class="table-responsive">
                    <table class="table table-condensed table-striped text-center">
                        <caption>Job(s) list</caption>
                        <thead>
                        <tr>
                            <th class="text-center">{{ 'Status'|trans }}</th>
                            <th class="text-center">{{ 'Job ID'|trans }}</th>
                            <th class="text-left">{{ 'Name'|trans }}</th>
                            <th class="text-center">{{ 'Type'|trans }}</th>
                            <th class="text-center">{{ 'Scheduled Time'|trans }}</th>
                            <th class="text-center">{{ 'Start time'|trans }}</th>
                            <th class="text-center">{{ 'End time'|trans }}</th>
                            <th class="text-center">{{ 'Elapsed time'|trans }}</th>
                            <th class="text-center">{{ 'Level'|trans }}</th>
                            <th class="text-center">{{ 'Bytes'|trans }}</th>
                            <th class="text-center">{{ 'Files'|trans }}</th>
                            <th class="text-center">{{ 'Speed'|trans }}</th>
                            <th class="text-center">{{ 'Compression'|trans }}</th>
                            <th class="text-center">{{ 'Pool'|trans }}</th>
                            <th class="text-center">{{ 'Log'|trans }}</th>
                        </tr>
                        </thead>

                        <!-- <div class="listbox"> -->
                        {% for job in last_jobs %}
                            <tr>
                                <td>
                                    <i title="{{ job.jobstatuslong }}" class="{{ job.Job_icon }}"></i>
                                </td>
                                <td>{{ job.jobid }}</td>
                                <td class="text-left">
                                    {% if job.type == 'B' %}
                                        <form action="{{ url_for('backupjob') }}" method="post">
                                            <input type="hidden" name="backupjob_name" value="{{ job.job_name }}"/>
                                            <input type="hidden" name="backupjob_period" value="7"/>
                                            <button type="submit"
                                                    class="btn btn-sm btn-link">{{ job.job_name }}</button>
                                            {{ csrf|raw }}
                                        </form>
                                    {% else %}
                                        {{ job.job_name }}
                                    {% endif %}
                                </td>
                                <td>{{ job.type }}</td>
                                <td>{{ job.schedtime }}</td>
                                <td>{{ job.starttime }}</td>
                                <td>{{ job.endtime }}</td>
                                <td>{{ job.elapsed_time }}</td>
                                <td>{{ job.level }}</td>
                                <td class="text-right">{{ job.jobbytes }}</td>
                                <td class="text-right">
                                    {% if job.jobfiles != 0 and job.type == 'B' %}
                                        <a href="{{ base_path() }}/jobfiles/{{ job.jobid }}"
                                           title="{{ 'Show job files'|trans }}">
                                            {{ job.jobfiles }} <i class="fa-solid fa-folder"></i>
                                        </a>
                                    {% else %}
                                        {{ job.jobfiles }}
                                    {% endif %}
                                </td>
                                <td>{{ job.speed }}</td>
                                <td>{{ job.compression }}</td>
                                <td>{{ job.pool_name }}</td>
                                <td>
                                    <a href="{{ base_path() }}/joblog/{{ job.jobid }}"
                                       title="{{ 'Show job logs'|trans }}"> <i
                                                class="fa-solid fa-magnifying-glass"></i> </a>
                                </td>
                            </tr>
                        {% else %}
                            <tr>
                                <td colspan="15">{{ 'No job(s) to display'|trans }}</td>
                            </tr>
                        {% endfor %}
                    </table>
                </div>

                {% if last_jobs|length > 0 %}
                    {{ include('partials/pagination.html.twig', {route: 'jobs' }) }}
                {% endif %}

            </div>
        </div>
    </div>

    <script type="text/javascript">
        const datetimepicker1 = new tempusDominus.TempusDominus(document.getElementById('datetimepicker1'), {
            localization: {
                locale: '{{ language }}',
                format: 'yyyy-MM-dd HH:mm:ss',
            }
        });

        const datetimepicker2 = new tempusDominus.TempusDominus(document.getElementById('datetimepicker2'), {
            localization: {
                locale: '{{ language }}',
                format: 'yyyy-MM-dd HH:mm:ss',
            }
        });
    </script>

{% endblock %}
